package com.ttg.web.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.common.entities.BaseResult;
import com.ttg.common.enums.*;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.JsonUtils;
import com.ttg.model.dto.EditConditionsTipsDto;
import com.ttg.model.dto.GetConditionClassByStatusDto;
import com.ttg.model.dto.GetConditionNameByClassDto;
import com.ttg.model.dto.ListConditionsTipsDto;
import com.ttg.model.mapper.ConditionsNameMapper;
import com.ttg.model.pojo.ConditionsName;
import com.ttg.model.pojo.ConditionsNameClass;
import com.ttg.model.pojo.VipGrowDetail;
import com.ttg.model.vo.*;
import com.ttg.web.dao.ConditionsExpressionDao;
import com.ttg.web.dao.ConditionsNameClassDao;
import com.ttg.web.dao.ConditionsNameDao;
import com.ttg.web.dao.DictDetailDao;
import com.ttg.web.service.ConditionsNameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/1/15 17:08
 */
@Slf4j
@Service
public class ConditionsNameServiceImpl extends ServiceImpl<ConditionsNameMapper, ConditionsName> implements ConditionsNameService {

    @Autowired
    private ConditionsNameDao conditionsNameDao;

    @Autowired
    private ConditionsExpressionDao conditionsExpressionDao;

    @Autowired
    private ConditionsNameClassDao conditionsNameClassDao;

    @Autowired
    private DictDetailDao dictDetailDao;

    @Override
    public List<ConditionVo> conditionList() {
        List<ConditionVo> conditionVos = new ArrayList<>();
        List<ConditionsName> conditionsNames = conditionsNameDao.list();
        if (ObjectUtil.isEmpty(conditionsNames)) {
            return conditionVos;
        }
        //获取数据类型下的表达式
        List<ExpressionVo> expressionVoList = conditionsExpressionDao.findExpressionsList();
        //判断条件是否存在表达式
        if (ObjectUtil.isEmpty(expressionVoList)) {
            conditionsNames.forEach(cn->{
                ConditionVo conditionVo = new ConditionVo();
                if(cn.getId().equals((long) ConditionsNameEnum.CONDITIONS_SEX.getValue())){
                    Map<Integer,String> bMap = new HashMap(2);
                    bMap.put(VipSexEnum.MAN.getValue(),"男性");
                    bMap.put(VipSexEnum.WOMAN.getValue(),"女性");
                    conditionVo.setBooleOption(bMap);
                    conditionVo.setRadio(1);
                }else if(cn.getId().equals((long) ConditionsNameEnum.CONDITIONS_SERVICE_IDENTIFICATION.getValue()) ){
                    conditionVo.setBooleOption(ServiceIdentificationEnum.map());
                    conditionVo.setRadio(2);
                }
                conditionVo.setValueType(cn.getValueType());
                conditionVo.setConditionsId(cn.getId());
                conditionVo.setConditionsName(cn.getConditionsName());
                conditionVo.setDataTypeId(cn.getConditionsDataTypeId());
                conditionVo.setShowExpression(cn.getShowExpression());
                conditionVo.setConditionsSuffixName(cn.getConditionsSuffixName());
                conditionVos.add(conditionVo);
            });
            return conditionVos;
        }
        //分组
        Map<Long, List<ExpressionVo>> map = expressionVoList.stream().collect(Collectors.groupingBy(ExpressionVo::getDataTypeId));

        conditionsNames.forEach(cn->{
            ConditionVo conditionVo = new ConditionVo();
            if(cn.getId().equals((long) ConditionsNameEnum.CONDITIONS_SEX.getValue())){
                Map<Integer,String> bMap = new HashMap(2);
                bMap.put(VipSexEnum.MAN.getValue(),"男性");
                bMap.put(VipSexEnum.WOMAN.getValue(),"女性");
                conditionVo.setBooleOption(bMap);
                conditionVo.setRadio(1);
            }else if(cn.getId().equals((long) ConditionsNameEnum.CONDITIONS_SERVICE_IDENTIFICATION.getValue()) ){
                conditionVo.setBooleOption(ServiceIdentificationEnum.map());
                conditionVo.setRadio(2);
            }
            conditionVo.setValueType(cn.getValueType());
            conditionVo.setConditionsId(cn.getId());
            conditionVo.setConditionsName(cn.getConditionsName());
            conditionVo.setDataTypeId(cn.getConditionsDataTypeId());
            conditionVo.setShowExpression(cn.getShowExpression());
            conditionVo.setExpressionVoList(map.get(cn.getConditionsDataTypeId()));
            conditionVo.setConditionsSuffixName(cn.getConditionsSuffixName());
            conditionVos.add(conditionVo);
        });
        return conditionVos;
    }

    @Override
    public List<ConditionsNameClassVo> getAllByStatus(GetConditionClassByStatusDto getConditionClassByStatusDto) {
        if (Objects.isNull(getConditionClassByStatusDto)|| StringUtils.isEmpty(getConditionClassByStatusDto.getType())){
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        List<ConditionsNameClass> allByStatus = conditionsNameClassDao.getAllByStatusAndType(Integer.valueOf(getConditionClassByStatusDto.getType()));
        List<ConditionsNameClassVo> conditionsNameClassVos = new ArrayList<>();
        for (ConditionsNameClass byStatus : allByStatus) {
            ConditionsNameClassVo vo = new ConditionsNameClassVo();
            vo.setId(byStatus.getId());
            vo.setClassName(byStatus.getClassName());
            conditionsNameClassVos.add(vo);
        }

        return conditionsNameClassVos;
    }

    @Override
    public List<GetConditionNameByClassVo> getConditionNameByClass(GetConditionNameByClassDto reqDto) {
        if (Objects.isNull(reqDto) || StringUtils.isEmpty(reqDto.getClassId())) {
            //throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        List<GetConditionNameByClassVo> conditionVos = new ArrayList<>();
        List<ConditionsName> conditionsNames = conditionsNameDao.findConditionsNameByClassId(reqDto.getClassId());
        if (ObjectUtil.isEmpty(conditionsNames)) {
          //  return conditionVos;
        }
        // 获取字典id
        List<Integer> dictIds = conditionsNames.stream().map(ConditionsName::getConditionsDictId).filter(Objects::nonNull).collect(Collectors.toList());

        Map<Integer, List<DictDetailVo>> mapByDictIds = dictDetailDao.listMapByDictIds(dictIds);


        //获取数据类型下的表达式
        List<ExpressionVo> expressionVoList = conditionsExpressionDao.findExpressionsList();
        //判断条件是否存在表达式
        if (ObjectUtil.isEmpty(expressionVoList)) {
            conditionsNames.forEach(cn -> {
                GetConditionNameByClassVo conditionVo = new GetConditionNameByClassVo();
                conditionVo.setBooleOption(cn.getBooleanValue());

                conditionVo.setValueType(cn.getValueType());
                conditionVo.setConditionsId(cn.getId());
                conditionVo.setConditionsName(cn.getConditionsName());
                conditionVo.setConditionsValueKey(cn.getConditionsValueKey());
                conditionVo.setDataTypeId(cn.getConditionsDataTypeId());
                conditionVo.setParamValidate(cn.getParamValidate());
                conditionVo.setShowExpression(cn.getShowExpression());
                conditionVo.setConditionsSuffixName(cn.getConditionsSuffixName());
                conditionVo.setRadio(cn.getRadio());
                conditionVo.setClassId(cn.getClassId());
                conditionVos.add(conditionVo);
            });
            return conditionVos;
        }
        //分组
        Map<Long, List<ExpressionVo>> map = expressionVoList.stream().collect(Collectors.groupingBy(ExpressionVo::getDataTypeId));

        conditionsNames.forEach(cn->{
            GetConditionNameByClassVo conditionVo = new GetConditionNameByClassVo();
            conditionVo.setBooleOption(cn.getBooleanValue());
            conditionVo.setValueType(cn.getValueType());
            conditionVo.setConditionsId(cn.getId());
            conditionVo.setParamValidate(cn.getParamValidate());
            conditionVo.setConditionsName(cn.getConditionsName());
            conditionVo.setConditionsValueKey(cn.getConditionsValueKey());
            conditionVo.setDataTypeId(cn.getConditionsDataTypeId());
            conditionVo.setShowExpression(cn.getShowExpression());
            conditionVo.setExpressionVoList(map.get(cn.getConditionsDataTypeId()));
            conditionVo.setConditionsSuffixName(cn.getConditionsSuffixName());
            conditionVo.setDictOption(mapByDictIds.get(cn.getConditionsDictId()));
            conditionVo.setTips(cn.getTips());
            conditionVo.setRadio(cn.getRadio());
            conditionVo.setClassId(cn.getClassId());
            // 由于前端没空,这里临时写一下以后可以删掉,暂时用BooleanValue字段生成radio勾选,前端下拉框做好后,可以删掉
            if (!CollectionUtils.isEmpty(conditionVo.getDictOption())) {
                try {
                    conditionVo.setBooleOption(JsonUtils.objectToJson(conditionVo.getDictOption()).replace("ddLabel", "name").replace("ddGxdValue", "value"));
                } catch (Exception ignored) {
                }
            }
            conditionVos.add(conditionVo);
        });
        return conditionVos;
    }

    @Override
    public BasePage<ConditionTipsVo> listConditionsTips(ListConditionsTipsDto reqDto) {
        BasePage<Object> parse = reqDto.parse();
        BasePage<ConditionTipsVo> page = new BasePage<>(parse.getCurrent(), parse.getSize(), parse.getTotal(), parse.isSearchCount());
        return conditionsNameDao.listConditionsTips(page,reqDto);
    }

    @Override
    public BaseResult editConditionsTips(EditConditionsTipsDto reqDto) {
        if (Objects.isNull(reqDto.getId())) {
            throw new BaseException(ErrorCodeEnums.SYSTEM_ERROR.getErrcode(), "字段ID不能为空");
        }
        if (StringUtils.isBlank(reqDto.getTips())) {
            throw new BaseException(ErrorCodeEnums.SYSTEM_ERROR.getErrcode(), "提示不能为空");
        }
        ConditionsName upd = new ConditionsName();
        upd.setId(reqDto.getId());
        upd.setTips(reqDto.getTips());
        upd.setTipsUpdTime(LocalDateTime.now());
        if (conditionsNameDao.updateById(upd)) {
            return BaseResult.success(ResultStatusEnum.SUCCESS);
        } else {
            return BaseResult.defaultFail();
        }
    }

    @Override
    public List<ConditionsNameClassVo> getAllConditionClass() {
        List<ConditionsNameClass> allByStatus = conditionsNameClassDao.getAllStatus0();
        List<ConditionsNameClassVo> conditionsNameClassVos = new ArrayList<>();
        for (ConditionsNameClass byStatus : allByStatus) {
            ConditionsNameClassVo vo = new ConditionsNameClassVo();
            vo.setId(byStatus.getId());
            vo.setClassName(byStatus.getClassName());
            conditionsNameClassVos.add(vo);
        }

        return conditionsNameClassVos;
    }
}
